
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>

#include <map>
#include <vector>
#include <sstream>
#include <iostream>
#include <unistd.h>

#include "interface.hpp"

#define logger(fmt, ...) \
    do { \
        time_t t; struct tm tm; struct timeb tp; \
        ftime(&tp); t = tp.time; tm = *localtime(&t);  tm = *localtime(&t); \
        printf("[%02d-%02d-%d %02d:%02d:%02d %03d]: ", tm.tm_mon + 1, \
                tm.tm_mday, tm.tm_year + 1900, tm.tm_hour, tm.tm_min, \
                tm.tm_sec, tp.millitm); \
        printf(fmt, ##__VA_ARGS__) ; fflush(stdout); \
    } while (0)


/* Convert integer data type to string */
template <class T>
std::string to_string(T d){
    std::stringstream s;
    s<<d;
    return s.str();
}

/* Execute commands */
void exec(Commands_info &cmds, Condition &c);

/* Print the 'response' vector */
void print_resp(std::vector<uint8_t> resp);

/* Tokenize the string */
void tokenize(const std::string& str,  const std::string& delimiters,
        std::vector<std::string>& tokens);

/* Find specified old_str in the str and replace it to new_str. */
void find_and_replace(std::string& str, const std::string& old_str, 
        std::string new_str);

/* Extract "g*" string from specified str. */
void extract_gids(const std::string str, std::vector<std::string>& cmds);

/* Evaluate expr and return the value. */
double eval_expr(std::string expr, std::string cnd_value);
double eval_expr2(std::string expr);

/* Replace the timers in the string (year, mon, day, hour, min, sec, mili). */
void replace_timers(std::string& s);

/* GSM */
int write_to_gsm(char *chars);
int read_from_gsm(char *result);

/* Handle client requests. */
std::string handle_request(std::string request);

/* parse serial config file and init serials. */
void parse_serial_config(std::string file_name);

/* initialize and start smarthome.*/
void init_and_start_core();

/* calculate check sum from the given base packet */
uint8_t calculate_csum(const b_packet_t* pkt);

/* convert string to packet structure */
b_packet_t* str_to_pkt(const std::string str);
